//
//
//import 'dart:ui';
//
//import 'package:bot_toast/bot_toast.dart';
//import 'package:cached_network_image/cached_network_image.dart';
//import 'package:dolqun_movies/Entities/LatestMovies.dart';
//import 'package:dolqun_movies/Entities/Movie.dart';
//import 'package:dolqun_movies/Entities/MovieDetailsResult.dart';
//import 'package:dolqun_movies/Interface/Network.dart';
//import 'package:dolqun_movies/StateManagement/ModelProvider.dart';
//import 'package:dolqun_movies/UI/Pages/HomePage/PlayerPage.dart';
//import 'package:dolqun_movies/UI/Pages/Models/ColorThemeModel.dart';
//import 'package:dolqun_movies/UI/Pages/Models/MainModel.dart';
//import 'package:dolqun_movies/UI/Pages/Models/WechatApiModel.dart';
//import 'package:dolqun_movies/UI/Pages/ProfilePage/BuyVipPage.dart';
//import 'package:dolqun_movies/UI/Widgets/Dialogs.dart';
//import 'package:dolqun_movies/UI/Widgets/EnsureHasSignin.dart';
//import 'package:dolqun_movies/UI/Widgets/MovieView.dart';
//import 'package:dolqun_movies/UI/Widgets/MyAnimatedRoute.dart';
//import 'package:dolqun_movies/UI/Widgets/MyEdgeClipper.dart';
//import 'package:flutter/cupertino.dart';
//import 'package:flutter/material.dart';
//import 'package:flutter/painting.dart';
//import 'package:wechat_kit/wechat_kit.dart';
//
//class DetailsPage extends StatefulWidget {
//  Movie movie;
//  String heroTag;
//  @override
//  State<StatefulWidget> createState() => _DetailsPageState();
//
//  DetailsPage({this.movie, this.heroTag}) {
//    heroTag ??= movie.id.toString();
//  }
//}
//
//class _DetailsPageState extends State<DetailsPage> {
//  var _controller = ScrollController();
//
//  Future<LatestResponse> _relatedMovies;
//
//  double getPosition(){
//    var position = 0.0;
//    if (_controller.positions.isNotEmpty) position = _controller.offset;
//    return position;
//  }
//
//  @override
//  Widget build(BuildContext context) {
//    var wechatApi = ModelContainer.of<WechatApiModel>(context).model;
//
//    Widget buildingWidget = null;
//    var model = ModelContainer.of<MainModel>(context).model;
//    var theme = ModelContainer.of<ColorThemeModel>(context).model;


import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dolqun_movies/Entities/LatestMovies.dart';
import 'package:dolqun_movies/Entities/Movie.dart';
import 'package:dolqun_movies/Entities/MovieDetailsResult.dart';
import 'package:dolqun_movies/StateManagement/ModelProvider.dart';
import 'package:dolqun_movies/UI/Pages/Models/ColorThemeModel.dart';
import 'package:dolqun_movies/UI/Pages/Models/MainModel.dart';
import 'package:dolqun_movies/UI/Pages/Models/WechatApiModel.dart';
import 'package:dolqun_movies/UI/Widgets/Dialogs.dart';
import 'package:dolqun_movies/UI/Widgets/EnsureHasSignin.dart';
import 'package:dolqun_movies/UI/Widgets/MovieView.dart';
import 'package:dolqun_movies/UI/Widgets/MyAnimatedRoute.dart';
import 'package:dolqun_movies/UI/Widgets/MyEdgeClipper.dart';
////    if (this.details == null) {
////      model.api.getMovieDetails(wechatApi.token, this.widget.movie.id).then((details){
////        setState(() {
////          this.details = details;
////          this._selectedSectionId = this.details.data.items.first.id;
////        });
////      });
////    }
//    var titleTextStyle = TextStyle(
//        fontSize: 18.0,
//        shadows: [Shadow(color: theme.textColor.withOpacity(0.4), blurRadius: 15.0),],
//        fontWeight: FontWeight.bold,
//        color: theme.textColor
//    );
//    buildingWidget = Scaffold(
//      body: Builder(
//        builder: (context) => Stack(
//          children: <Widget>[
//            Positioned.fill(
//              child: AnimatedBuilder(
//                animation: _controller,
//                builder: (context, _){
//                  var opacity = 0.0;
//                  opacity = (getPosition() / 100);
//                  if (opacity < 0) opacity = 0;
//                  if (opacity > 1) opacity = 1;
//                  return Container(
//                      color: Color.lerp(Theme.of(context).scaffoldBackgroundColor, Color(0xff080613), opacity)
//                  );
//                },
//              ),
//            ),
//            Positioned.fill(
//              child: FutureBuilder<MovieDetailsResult>(
//                future: model.api.getMovieDetails(wechatApi.token, this.widget.movie.id),
//                builder: (context, snapshot) {
//                  if (snapshot.hasError) return Center(
//                    child: Text(snapshot.error.toString()),
//                  );
//                  var details = snapshot.data;
//                  return ListView(
//                    controller: _controller,
//                    padding: EdgeInsets.only(top: 0.0),
//                    children: <Widget>[
//                      SizedBox(
//                          height: 370,
//                          child: Stack(
//                            children: <Widget>[
//                              Positioned.fill(
//                                child: ClipPath(
//                                  clipper: MyEdgeClipper(clipHeight: 20.0),
//                                  child: CachedNetworkImage(imageUrl: widget.movie.image, fit: BoxFit.cover,),
//                                ),
//                              ),
//                              Positioned.fill(
//                                child: Container(
//                                  margin: EdgeInsets.all(10.0),
//                                  child: Column(
//                                    mainAxisAlignment: MainAxisAlignment.center,
//                                    children: <Widget>[
//                                      SizedBox(height: 10.0,),
//                                      Expanded(
//                                        child: AnimatedBuilder(
//                                          animation: _controller,
//                                          child: Container(
//                                            decoration: BoxDecoration(
//                                                boxShadow: [
//                                                  BoxShadow(spreadRadius: 70.0, blurRadius: 70.0, color: Colors.black.withOpacity(0.8))
//                                                ]
//                                            ),
//                                            child: AspectRatio(
//                                              aspectRatio: 0.8,
//                                              child: ClipRRect(
//                                                borderRadius: BorderRadius.circular(15.0),
//                                                child: Hero(
//                                                  tag: widget.heroTag,
//                                                  child: Stack(
//                                                    children: <Widget>[
//                                                      Positioned.fill(
//                                                        child: CachedNetworkImage(imageUrl: widget.movie.image, fit: BoxFit.cover,),
//                                                      ),
//                                                    ],
//                                                  ),
//                                                ),
//                                              ),
//                                            ),
//                                          ),
//                                          builder: (context, widget){
//                                            var scale = 1.0;
//                                            scale = scale - getPosition() / 200;
//                                            if (scale < 0) scale = 0;
//                                            var matrix = Matrix4.diagonal3Values(scale, scale, 1.0);
//                                            var rotation = 3.14 / 2 - scale * 3.14 / 2;
//                                            if (rotation < 0) rotation = 0;
//                                            matrix.rotateZ(rotation);
//                                            return Transform(
//                                              transform: matrix,
//                                              alignment: Alignment.center,
//                                              child: widget,
//                                            );
//                                          },
//                                        ),
//                                      ),
//                                    ],
//                                  ),
//                                ),
//                              ),
//                              Transform.translate(
//                                offset: Offset(0.0, 30.0),
//                                child: IconButton(
//                                  icon: Icon(Icons.arrow_back_ios, color: Colors.white,),
//                                  onPressed: (){
//                                    Navigator.of(context).pop();
//                                  },
//                                ),
//                              ),
//                              Positioned(
//                                bottom: 0.0,
//                                right: 40.0,
//                                child: InkWell(
//                                  child: Container(
//                                    width: 60.0,
//                                    height: 60.0,
//                                    decoration: BoxDecoration(
//                                      color: Colors.blue,
//                                      borderRadius: BorderRadius.circular(30.0),
//                                    ),
//                                    child: Icon(Icons.play_arrow, color: Colors.white, size: 46.0,),
//                                  ),
//                                  onTap: (){
//                                    if (details == null) {
//                                      BotToast.showText(text: "Please wait few second and try again...");
//                                      return;
//                                    }
//                                    Navigator.of(context).push(MyAnimatedRoute(builder: (context) => PlayerPage(details: details.data,),));
//                                  },
//                                ),
//                              )
//                            ],
//                          )
//                      ),
//                      SizedBox(height: 20.0,),
//                      Text(this.widget.movie.name, style: titleTextStyle,),
//                      SizedBox(width: 10),
//                      Wrap(
//                        crossAxisAlignment: WrapCrossAlignment.center,
//                        children: <Widget>[
//                          _buildBadge(details?.data?.views?.toString(), Icons.remove_red_eye),
//                          SizedBox(width: 5),
//                          _buildBadge(details?.data?.likes?.toString(), Icons.favorite_border),
//                          SizedBox(width: 5),
//                          _buildBadge(details?.data?.moments?.toString(), Icons.panorama_fish_eye),
//                        ],
//                      ),
//                      if ((details?.data?.alias ?? "") != "") Text("باشقىلار بۇ فىلىمنى يەنە [${details?.data?.alias}] دەپ ئاتايدۇ"),
//                      SizedBox(height: 10),
//                      Center(
//                        child: Container(
//                          height: 100.0,
//                          width: 360.0,
//                          margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
//                          padding: EdgeInsets.all(10.0),
//                          decoration: BoxDecoration(
//                              color: Colors.black.withOpacity(0.2),
//                              borderRadius: BorderRadius.circular(15.0)
//                          ),
//                          child: Row(
//                            children: <Widget>[
//                              Flexible(
//                                child: Column(
//                                  mainAxisAlignment: MainAxisAlignment.spaceAround,
//                                  children: <Widget>[
//                                    _buildKeyValuePair("تەرجىمىدە:", details?.data?.author ?? "Loading..."),
//                                    _buildKeyValuePair("تەھرىر: ", "پۇستانى"),
//                                    _buildKeyValuePair("كىرىشتۈرگۈچى: ", "ئامانى"),
//                                  ],
//                                ),
//                              ),
//                              Flexible(
//                                child: Column(
//                                  mainAxisAlignment: MainAxisAlignment.spaceAround,
//                                  children: <Widget>[
//                                    Row(
//                                      mainAxisAlignment: MainAxisAlignment.spaceAround,
//                                      children: <Widget>[
//                                        Text(widget.movie.rate.toString()),
//                                        SizedBox(width: 10.0,),
//                                        for (var value in widget.movie.stars())
//                                              (){
//                                            var icon = Icons.star_border;
//                                            if (value == StarValue.Middle) icon = Icons.star_half;
//                                            if (value == StarValue.High) icon = Icons.star;
//                                            return Icon(icon, color: Colors.yellow);
//                                          }(),
//                                      ],
//                                    ),
//                                    Row(
//                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
//                                      children: <Widget>[
//                                        Icon(Icons.share, color: Colors.yellow, size: 28.0,),
////                                    IconButton(
////                                      icon: Icon(Icons.thumb_up, color: Colors.yellow, size: 28.0),
////                                      onPressed: (){
////
////                                      },
////                                    ),
//                                        Builder(
//                                          builder: (context) {
//                                            var inFav = false;
//                                            return StatefulBuilder(
//                                              builder: (context, setState) => IconButton(
//                                                icon: Icon(inFav ? Icons.star : Icons.star_border, color: Colors.yellow, size: 28.0),
//                                                onPressed: ()async {
//                                                  await EnsureHasSignIn.runWhenSignIn(context, () async {
//                                                    BotToast.showLoading();
//                                                    await Dialogs.runWithDialogsWhenErrorOccurres(context, () async {
//                                                      var result = await model.api.favoriteUnfavoriteMovie(wechatApi.token, details.data.id);
//                                                      setState(() {
//                                                        inFav = result;
//                                                      });
//                                                    });
//                                                    BotToast.closeAllLoading();
//                                                  });
//                                                },
//                                              ),
//                                            );
//                                          },
//                                        ),//                                    Icon(Icons.cloud_download, color: Colors.yellow, size: 28.0),
//                                      ],
//                                    )
//                                  ],
//                                ),
//                              )
//                            ],
//                          ),
//                        ),
//                      ),
////                  Text("قىسىملىرى (${details?.data?.items?.length ?? 0})", style: titleTextStyle,),
//                      SizedBox(height: 10),
//                      Text("فىلىم ھەققىدە:", style: titleTextStyle,),
//                      Text(details?.data?.remark ?? "Loading"),
//                      Text("باشقا تەۋسىيە فىلىملەر", style: titleTextStyle,),
//                      SizedBox(
//                        height: 200.0,
//                        width: double.infinity,
//                        child: FutureBuilder<LatestResponse>(
//                          future: this._relatedMovies ??= model.api.getRelatedMovies(widget.movie.id),
//                          builder: (context, snapshot){
//                            if (snapshot.hasError) return Center(child: Text(snapshot.error.toString()));
//                            if (snapshot.hasData) return ListView(
//                              scrollDirection: Axis.horizontal,
//                              children: snapshot.data.data.map((item) => _buildMovieItem(item, heroTagBuilder: (id) => "RelatedMovie:$id"),).toList(),
//                            );
//                            return Center(child: CircularProgressIndicator());
//                          },
//                        ),
//                      ),
//                      Container(
//                        height: 200.0,
//                      )
//                    ],
//                  );
//                },
//              ),
//            ),
//            Positioned(
//              bottom: 0,
//              left: 0,
//              right: 0,
//              child: ClipRect(
//                child: BackdropFilter(
//                  filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
//                  child: Builder(
//                    builder: (context){
//                      var expanded = false;
//                      return StatefulBuilder(
//                        builder: (context, setState) => AnimatedContainer(
//                          height: expanded ? 100.0 : 50.0,
//                          duration: Duration(milliseconds: 100),
//                          color: Colors.white.withOpacity(0.04),
//                          child: Row(
//                            children: <Widget>[
//                              SizedBox(width: 20),
//                              _buildLittlePanel(Icon(Icons.send, color: Colors.white),),
//                              SizedBox(width: 5),
//                              _buildLittlePanel(InkWell(
//                                child: Icon(expanded ? Icons.keyboard_arrow_down : Icons.keyboard_arrow_up, color: Colors.white),
//                                onTap: (){
//                                  setState(() {
//                                    expanded = !expanded;
//                                  });
//                                },
//                              ),),
//                              SizedBox(width: 5),
//                              Expanded(
//                                  child: Padding(
//                                    padding: EdgeInsets.symmetric(vertical: 5.0),
//                                    child: _buildLittlePanel(TextField(
//                                      style: TextStyle(color: Colors.white),
//                                      maxLines: 3,
//                                      decoration: InputDecoration(
//                                          hintText: "ئۇنى بۇنى دەپ قويمامسىز؟",
//                                          hintStyle: TextStyle(color: Colors.white30),
//                                          enabledBorder: InputBorder.none,
//                                          focusedBorder: InputBorder.none
//                                      ),
//                                    )),
//                                  )
//                              ),
//                              SizedBox(width: 20),
//                            ],
//                          ),
//                        ),
//                      );
//                    },
//                  ),
//                ),
//              ),
//            )
//          ],
//        ),
//      )
//    );
//    buildingWidget = Directionality(
//      textDirection: TextDirection.rtl,
//      child: buildingWidget,
//    );
//    return buildingWidget;
//  }
//
//  Widget _buildKeyValuePair(String key, String value) {
//    var style = TextStyle(
//        color: Colors.white.withOpacity(0.4),
//      fontSize: 12.0
//    );
//    return Row(
//      mainAxisAlignment: MainAxisAlignment.spaceBetween,
//      children: <Widget>[
//        Text(key, style: style,),
//        SizedBox(width: 8.0,),
//        Spacer(),
//        Text(value, style: style,),
//        SizedBox(width: 60.0,)
//      ],
//    );
//  }
//
//  Widget _buildMovieItem(Movie movie, {String heroTagBuilder(int id)}) {
//    heroTagBuilder ??= (id) => id.toString();
//    var tag = heroTagBuilder(movie.id);
//    return Hero(
//      tag: tag,
//      child: MovieView(
//        movie: movie,
//        onTab: ()async {
//          await Navigator.of(context).push(MyAnimatedRoute(builder: (context) => DetailsPage(movie: movie, heroTag: tag,)));
//        },
//      ),
//    );
//  }
//
//  Widget _buildBadge(String text, IconData icon) {
//    var theme = ModelContainer.of<ColorThemeModel>(context).model;
//    return Container(
//      decoration: BoxDecoration(
//        color: theme.textColor,
//        borderRadius: BorderRadius.circular(8.0),
//      ),
//      padding: EdgeInsets.all(3.0),
//      child: Row(
//        mainAxisSize: MainAxisSize.min,
//        mainAxisAlignment: MainAxisAlignment.spaceAround,
//        children: <Widget>[
//          Icon(icon, size: 18.0, color: Colors.white,),
//          Text(text ?? "..."),
//        ],
//      ),
//    );
//  }
//
//  Widget _buildLittlePanel(Widget content) {
//    return Container(
//      decoration: BoxDecoration(
//        color: Colors.black.withOpacity(0.1),
//        borderRadius: BorderRadius.circular(8)
//      ),
//      constraints: BoxConstraints(
//        minHeight: 40.0,
//        minWidth: 40.0
//      ),
//      child: content,
//    );
//  }
//
//  Widget _ListTile({Widget title, Widget subtitle, Widget leading, Widget trailing, void onTap()}){
//    return InkWell(
//      onTap: onTap,
//      child: Row(
//        children: [
//          SizedBox(width: 10.0,),
//          leading,
//          SizedBox(width: 10.0,),
//          Column(
//            crossAxisAlignment: CrossAxisAlignment.start,
//            children: [
//              AnimatedDefaultTextStyle(
//                style: Theme.of(context).textTheme.bodyText1.copyWith(fontWeight: FontWeight.bold, fontSize: 16.0, color: Colors.white),
//                duration: Duration(seconds: 1),
//                child: title,
//              ),
//              AnimatedDefaultTextStyle(
//                style: Theme.of(context).textTheme.bodyText1.copyWith(fontWeight: FontWeight.bold, fontSize: 12.0, color: Colors.white70),
//                duration: Duration(seconds: 1),
//                child: subtitle,
//              ),
//            ],
//          ),
//          Spacer(),
//          trailing,
//          SizedBox(width: 10.0,),
//        ],
//      ),
//    );
//  }
//}
//

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'dart:math' as Math;


class DetailsPage extends StatefulWidget {
  final Movie movie;
  final String heroTag;
  DetailsPage({this.movie, this.heroTag});

  @override
  State<StatefulWidget> createState() => _DetailsPageState();
}

class _DetailsPageState extends State<DetailsPage> {
  Future<MovieDetailsResult> details;
  final _scrollController = ScrollController();
  final _pageViewController = PageController();
  final _panelKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    final api = ModelContainer.of<MainModel>(context).model.api;
    this.details ??= api.getMovieDetails(ModelContainer.of<WechatApiModel>(context).model.token, this.widget.movie.id);

    var isPanelExpanded = false;
    var partPanelHeight = 1000.0;
    var panelIsMeasured = false;
    Widget buildingWidget = Scaffold(
      body: Stack(
        children: <Widget>[
          Positioned.fill(
            child: ListView(
              controller: _scrollController,
              padding: EdgeInsets.only(top: 0.0),
              children: <Widget>[
                SizedBox(height: 60.0 + 320.0,),
                StatefulBuilder(
                  builder: (context, setState) {
                    return AnimatedContainer(
                      duration: Duration(milliseconds: 600),
                      curve: Curves.elasticOut,
                      height: isPanelExpanded ? partPanelHeight : 280.0,
                      child: Stack(
                        children: <Widget>[
                          Positioned.fill(
                            child: Container(
                              margin: EdgeInsets.all(15.0),
                              decoration: BoxDecoration(
                                  color: Color(0xff13171f),
                                  borderRadius: BorderRadius.circular(15.0)
                              ),
                              child: PageView(
                                controller: _pageViewController,
                                children: <Widget>[
                                  _buildPartPanel(
                                    onDataReady: (data){
                                      if (!panelIsMeasured) {
                                        SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
                                          setState(() {
                                            final panelWidth = (_panelKey.currentContext?.findRenderObject() as RenderBox)?.size?.width ?? 1000.0;
                                            final horizontalItemCount = panelWidth / ((6 * 2) + 60).floor();
                                            var verticalCount = data.length / horizontalItemCount;
                                            verticalCount++;
                                            partPanelHeight = verticalCount * (60 + 10 * 2);
                                            if (partPanelHeight < 280) partPanelHeight = 300;
                                          });
                                        });
                                        panelIsMeasured = true;
                                      }
                                    }
                                  ),
                                  _buildDetailsPanel(),
                                ],
                              ),
                            ),
                          ),
                          Positioned(
                            top: 0.0,
                            right: 0.0,
                            left: 0.0,
                            height: 50.0 + (10 * 2),
                            child: Center(
                              child: Transform.translate(
                                offset: Offset(0.0, -20),
                                child: Container(
                                  margin: EdgeInsets.all(10.0),
                                  width: 250.0,
                                  decoration: BoxDecoration(
                                      color: Color(0xff1c1f26),
                                      borderRadius: BorderRadius.circular(15.0)
                                  ),
                                  child: AnimatedBuilder(
                                    animation: _pageViewController,
                                    builder: (context, child) => Row(
                                      children: <Widget>[
                                        SizedBox(width: 15.0,),
                                        _buildSelectionButton("قىسىم تاللاش",
                                          selected: (_pageViewController.page ?? 0.0) < 0.5,
                                          onTap: () => _pageViewController.animateToPage(0, duration: Duration(milliseconds: 800), curve: Curves.elasticOut),
                                        ),
                                        Spacer(),
                                        _buildSelectionButton("فىلىم ھەققىدە",
                                          selected: (_pageViewController.page ?? 0.0) > 0.5,
                                          onTap: () => _pageViewController.animateToPage(1, duration: Duration(milliseconds: 800), curve: Curves.elasticOut),
                                        ),
                                        SizedBox(width: 15.0,),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ),
                          Positioned(
                            left: 0.0,
                            bottom: 0.0,
                            right: 0.0,
                            height: 35.0,
                            child: Center(
                              child: Container(
                                width: 100.0,
                                decoration: BoxDecoration(
                                    color: Color(0xff1c1f26),
                                    borderRadius: BorderRadius.circular(12.0)
                                ),
                                child: FlatButton(
                                  child: Transform.rotate(
                                    angle: isPanelExpanded ? 3.14 / 2 : -(3.14 / 2),
                                    child: Icon(Icons.arrow_forward_ios, color: Colors.white,),
                                  ),
                                  onPressed: () {
//                                  final renderObject = _partKey.currentContext.findRenderObject() as RenderWrap;
                                    setState(() {
                                      isPanelExpanded = !isPanelExpanded;
                                      if (isPanelExpanded) {
                                        final destinationPosition = _scrollController.offset + 200;
                                        _scrollController.animateTo(destinationPosition, duration: Duration(seconds: 1), curve: Curves.easeOutQuart);
                                      }
                                    });
                                  },
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    );
                  },
                ),
                Text("باشقا تەۋسىيە فىلىملەر"),
                SizedBox(
                  height: 200.0,
                  width: double.infinity,
                  child: FutureBuilder<LatestResponse>(
                    future: ModelContainer.of<MainModel>(context).model.api.getRelatedMovies(widget.movie.id),
                    builder: (context, snapshot){
                      if (snapshot.hasError) return Center(child: Text(snapshot.error.toString()));
                      if (snapshot.hasData) return ListView(
                        scrollDirection: Axis.horizontal,
                        children: snapshot.data.data.map((item) => _buildMovieItem(item, heroTagBuilder: (id) => "RelatedMovie:$id"),).toList(),
                      );
                      return Center(child: CircularProgressIndicator());
                    },
                  ),
                ),
              ],
            ),
          ),
          Positioned(
            top: 0.0,
            left: 0.0,
            right: 0.0,
            height: 320.0,
            child: _buildCovers(),
          ),
        ],
      ),
    );
    buildingWidget = Directionality(textDirection: TextDirection.rtl, child: buildingWidget);
    return buildingWidget;
  }

  Widget _buildCovers() {
    return Stack(
      children: <Widget>[
        Positioned.fill(
          child: AnimatedBuilder(
            animation: this._scrollController,
            child: ClipPath(
              clipper: MyEdgeClipper(clipHeight: 30.0),
              child: Stack(
                children: <Widget>[
                  Positioned.fill(child: CachedNetworkImage(imageUrl: this.widget.movie.image, fit: BoxFit.cover,),),
                  Positioned.fill(
                    child: ClipRRect(
                      child: BackdropFilter(
                        filter: ImageFilter.blur(sigmaY: 20.0),
                        child: Padding(
                          padding: EdgeInsets.only(right: 30.0),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              SizedBox(height: MediaQuery.of(context).padding.top + 10),
                              Text(this.widget.movie.name, style: TextStyle(fontSize: 18.0, color: Color(0xffecf5ff,), fontWeight: FontWeight.bold, shadows: [Shadow(color: Colors.black, blurRadius: 10.0)]),),
                              SizedBox(height: 5.0,),
                              FutureBuilder<MovieDetailsResult>(
                                future: this.details,
                                builder: (context, snapshot) {
                                  if (snapshot.hasError) return Text("An error occurred while loading data, here is the error message: ${snapshot.error}");
                                  if (snapshot.hasData) return Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    mainAxisSize: MainAxisSize.min,
                                    children: <Widget>[
                                      Text("باشقىلار بۇ فىلىمنى يەنە [${snapshot.data.data?.alias ?? ""}] دەپ ئاتايدۇ", style: TextStyle(fontSize: 15.0, color: Color(0xffdedede,), shadows: [Shadow(color: Colors.black, blurRadius: 10.0)]),),
                                      SizedBox(height: 80.0,),
                                      SizedBox(
                                        width: 150.0,
                                        child: Text("${snapshot.data.data.extra}", style: TextStyle(shadows: [Shadow(color: Colors.black, blurRadius: 10.0)], color: Colors.white),),
                                      ),
                                    ],
                                  );
                                  return Text("Loading...");
                                },
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            builder: (context, child) {
              var opacity = (_scrollController.offset) / 150;
              opacity = 1.0 - opacity;
              if (opacity > 1.0) opacity = 1.0;
              if (opacity < 0.0) opacity = 0.0;
              return Transform.translate(
                offset: Offset(0.0, -(_scrollController.offset / 3)),
                child: Opacity(
                  opacity: opacity,
                  child: child,
                ),
              );
            },
          ),
        ),
//        Container(
//          width: 100.0,
//          height: 160.0,
//          decoration: BoxDecoration(
//              boxShadow: [
//                BoxShadow(color: Colors.black, spreadRadius: 30.0, blurRadius: 30.0)
//              ]
//          ),
//        ),
        Positioned(
          top: 0.0,
          height: 300.0,
          left: 0.0,
          right: 0.0,
          child: Align(
            alignment: Alignment.bottomLeft,
            child: AnimatedBuilder(
              animation: this._scrollController,
              child: Transform.translate(
                offset: Offset(30.0, 30.0),
                child: ClipRRect(
                  borderRadius: BorderRadius.only(
                    topRight: Radius.circular(20.0),
                    bottomRight: Radius.circular(20.0),
                    bottomLeft: Radius.circular(20.0),
                  ),
                  child: Hero(
                    tag: this.widget.heroTag,
                    child: GestureDetector(
                      child: CachedNetworkImage(imageUrl: this.widget.movie.image, width: 160.0, height: 230, fit: BoxFit.scaleDown,),
                      onTap: () {
                        this._scrollController.animateTo(0.0, duration: Duration(milliseconds: 200), curve: Curves.easeOutQuart);
                      },
                    ),
                  ),
                ),
              ),
              builder: (context, child) {
                var rotationValue = _scrollController.offset / 100;
                if (rotationValue > 0.7) rotationValue = 0.7;
                if (rotationValue < 0) rotationValue = 0;

                var translateX = _scrollController.offset;
                if (translateX > 120) translateX = 120;
                if (translateX < 0) translateX = 0;
                var scale = (-_scrollController.offset * (1 /50)) + 1.0;
                if (scale < 0.7) scale = 0.7;
                return Transform.translate(
                  offset: Offset(-translateX, 0.0),
                  child: Transform.rotate(
                    angle: rotationValue,
                    child: Transform.scale(
                      scale: scale,
                      child: child,
                    ),
                  ),
                );
              },
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildSelectionButton(String text, {bool selected = false, void onTap()}) {
    Widget buildingWidget = AnimatedContainer(
      duration: Duration(milliseconds: 200),
      width: 90.0,
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 7.0),
      decoration: BoxDecoration(
        color: selected ? Color(0xff0366d8) : Color(0xff13171f),
        borderRadius: BorderRadius.circular(15.0),
      ),
      child: Text(text, style: TextStyle(color: Colors.white),),
    );
    buildingWidget = InkWell(
      child: buildingWidget,
      onTap: onTap,
    );
    buildingWidget = Align(child: buildingWidget,);
    return buildingWidget;
  }

  Widget _buildDetailsPanel() {
    return FutureBuilder<MovieDetailsResult>(
      future: this.details,
      builder: (context, snapshot) {
        if (snapshot.hasData) return Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          child: Column(
            children: <Widget>[
              SizedBox(height: 30.0,),
              Expanded(
                child: Text(snapshot.data.data.remark, overflow: TextOverflow.ellipsis, maxLines: 6),
              ),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 3.0),
                height: 40.0,
                decoration: BoxDecoration(
                  color: Color(0xff010712),
                  borderRadius: BorderRadius.circular(15.0)
                ),
                alignment: Alignment.center,
                child: Text(snapshot.data.data.author, style: TextStyle(fontSize: 12.0),),
              ),
              SizedBox(height: 40.0,)
            ],
          ),
        );
        return Text("Loading...");
      },
    );
  }

  Widget _buildMovieItem(Movie movie, {String heroTagBuilder(int id)}) {
    heroTagBuilder ??= (id) => id.toString();
    var tag = heroTagBuilder(movie.id);
    return Hero(
      tag: tag,
      child: MovieView(
        movie: movie,
        onTab: ()async {
          await Navigator.of(context).push(MyAnimatedRoute(builder: (context) => DetailsPage(movie: movie, heroTag: tag,)));
        },
      ),
    );
  }

  Widget _buildPartPanel({void onDataReady(List<MovieDetailsSection> data)}) {
    Widget buildingWidget = FutureBuilder<MovieDetailsResult>(
      future: this.details,
      builder: (context, snapshot) {
        if (snapshot.hasError) return Center(child: Text(snapshot.error.toString()),);
        if (snapshot.hasData) {
          onDataReady(snapshot.data.data.items);
          return Stack(
            children: <Widget>[
              Positioned.fill(
                child: Wrap(
                  key: _panelKey,
                  children: snapshot.data.data.items.map((part) => _buildPartItem(part)).toList(),
                ),
              ),
              Positioned(
                left: 0.0,
                right: 0.0,
                bottom: 0.0,
                height: 130.0,
                child: Container(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: [Colors.black, Colors.transparent],
                      begin: Alignment.bottomCenter,
                      end: Alignment.topCenter,
                    ),
                  ),
                ),
              ),
            ],
          );
        }
        return Center(
          child: Text("Loading..."),
        );
      },
    );
    buildingWidget = Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        SizedBox(height: 30.0,),
        Expanded(child: buildingWidget,),
      ],
    );
    return buildingWidget;
  }

  Widget _buildPartItem(MovieDetailsSection part) {
    Widget buildingWidget = Text(part.name);
    buildingWidget = Container(
      width: double.infinity,
      height: double.infinity,
      child: buildingWidget,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: Colors.deepPurple.withOpacity(0.1),
        borderRadius: BorderRadius.circular(12.0),
      ),
      margin: EdgeInsets.symmetric(horizontal: 6.0, vertical: 10.0),
    );
    buildingWidget = Stack(
      children: <Widget>[
        Positioned.fill(child: buildingWidget),
        Positioned(
          top: 0.0,
          left: 0.0,
          child: part.buy ? SizedBox() : Image.asset("assets/images/DolqunVIP.png", width: 20.0, height: 20.0,),
        ),
      ],
    );
//    print("Cost: ${part.cost}, price: ${part.price}, isBuy: ${part.buy}");
    buildingWidget = SizedBox(width: 60.0, height: 60.0, child: buildingWidget,);
    buildingWidget = InkWell(
      child: buildingWidget,
      onTap: () {
        final userModel = ModelContainer.of<WechatApiModel>(context).model;
        final hasSignIn = userModel.userInfo != null;
        EnsureHasSignIn.runWhenSignIn(context, () {
          if (!hasSignIn) {
            setState(() {
              this.details = null;
            });
            return;
          }
          if (!part.buy) {

          }
        });
      },
    );
    return buildingWidget;
  }
}
